package com.haven.android.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.applovin.sdk.AppLovinSdkSettings;
import com.haven.android.sdk.max.HavenMaxReward;
import com.haven.android.sdk.utils.HavenAesUtils;

public class GameApplication {

    public static GameApplication getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final GameApplication GAME_APPLICATION = new GameApplication();
    }

    private void registerActivityLifecycleCallbacks(Application app) {
        app.registerActivityLifecycleCallbacks(new GameActivityLifecycleCallbacks());
    }

    public void onCreate(Activity activity) {
        Log.e("HavenW", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();
        registerActivityLifecycleCallbacks((Application) mApplication);
        HavenBusiness.init(mApplication);

        //AppLovinPrivacySettings
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);
        //AppLovinSdkSettings
        AppLovinSdkSettings settings = AppLovinSdk.getInstance(mApplication).getSettings();
        //settings.setVerboseLogging(true);
        settings.setExtraParameter("enable_black_screen_fixes", "true");
        // Create the initialization configuration
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        HavenAesUtils.decrypt(Constant.MAX_KEY), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        // Initialize the SDK with the configuration
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                Log.e("HavenW", "onSdkInitialized");
                HavenMaxReward.getInstance().loadAd(mApplication);
            }
        });

    }
}
